package com.project.junM.service.impl;

import com.project.junM.entity.TMember;
import com.project.junM.entity.TVaccinDept;
import com.project.junM.entity.TVaccinNote;
import com.project.junM.entity.TVaccinNoteDept;
import com.project.junM.mapper.TVaccinDeptMapper;
import com.project.junM.mapper.TVaccinNoteDeptMapper;
import com.project.junM.mapper.TVaccinNoteMapper;
import com.project.junM.service.VaccinNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class VaccinNoteServiceImpl implements VaccinNoteService {

    @Autowired
    private TVaccinNoteMapper tVaccinNoteMapper;
    @Autowired
    private TVaccinDeptMapper tVaccinDeptMapper;
    @Autowired
    private TVaccinNoteDeptMapper tVaccinNoteDeptMapper;

    public boolean insertSelective(TVaccinNote vaccinNote, HttpSession session){
        TMember user = (TMember)session.getAttribute("user");
        boolean flag = user!=null?true:false;
        System.out.println(vaccinNote);
        if(flag) {
            Integer id = user.getId();
            //查询当前用户之前是否预约过
            List<TVaccinNote> tVaccinNotes = tVaccinNoteMapper.selectByMemberId(id,vaccinNote.getVaccinIdCard());
            Integer vaccinCount = tVaccinNotes.size()!=0 ? tVaccinNotes.get(tVaccinNotes.size() - 1).getVaccinCount() + 1 : 1;
            //通过疫苗机构id,查出机构信息
            Integer deptId = vaccinNote.getVaccinDept();
            TVaccinDept tVaccinDept = tVaccinDeptMapper.selectByPrimaryKey(deptId);
//            TVaccinNote tVaccinNote = new TVaccinNote();
            vaccinNote.setMemberId(id);
            vaccinNote.setVaccinCount(vaccinCount);
            vaccinNote.setVaccinAddress(tVaccinDept.getDeptAddress());
            vaccinNote.setVaccinDate(vaccinNote.getVaccinDate());
            tVaccinNoteMapper.insertSelective(vaccinNote);
            //当天预约总人数
            TVaccinNoteDept tVaccinNoteDept1 = tVaccinNoteDeptMapper.selectByDeptId(deptId);
            TVaccinNoteDept tVaccinNoteDept = new TVaccinNoteDept();
            tVaccinNoteDept.setDeptId(deptId);
            int i = tVaccinNoteDept1.getOrderCount() != 0 ? tVaccinNoteDept1.getOrderCount() + 1 : 1;
            tVaccinNoteDept.setOrderCount(i);
            tVaccinNoteDeptMapper.updateByPrimaryKeySelective(tVaccinNoteDept);
//            tVaccinNoteMapper.insertSelective(vaccinNote);
        }
        return flag;
    }

    @Override
    public boolean selectByVaccinIdCard1(String vaccinIdCard) {

        List<TVaccinNote> tVaccinNotes = tVaccinNoteMapper.selectByVaccinIdCard(vaccinIdCard);
        if(tVaccinNotes.size()>0){
            Integer vaccinDept = tVaccinNotes.get(tVaccinNotes.size() - 1).getVaccinDept();

            Integer vaccinCount = tVaccinNotes.get(tVaccinNotes.size() - 1).getVaccinCount();

            TVaccinDept tVaccinDept = tVaccinDeptMapper.selectByPrimaryKey(vaccinDept);

            Integer setCount = tVaccinDept.getSetCount();
            System.out.println(setCount);
            System.out.println(vaccinCount);
            boolean flag=vaccinCount==setCount?true:false;

            return flag;
        }else {
            return false;
        }
    }

    //未写完
    @Override
    public boolean selectByVaccinIdCard2(String vaccinIdCard,Integer vaccinDeptId,Integer vaccinCount) {

        List<TVaccinNote> tVaccinNotes = tVaccinNoteMapper.selectByVaccinIdCard1(vaccinIdCard,vaccinDeptId,vaccinCount);
        if(tVaccinNotes.size()>0){
            /*Integer vaccinDept = tVaccinNotes.get(tVaccinNotes.size() - 1).getVaccinDept();
//            Integer vaccinCount = tVaccinNotes.get(tVaccinNotes.size() - 1).getVaccinCount();
            TVaccinDept tVaccinDept = tVaccinDeptMapper.selectByPrimaryKey(vaccinDept);
//            Integer setCount = tVaccinDept.getSetCount();
            boolean flag=tVaccinDept!=null?true:false;
            return flag;*/
            return false;
        }else {

            return true;
        }
    }


}
